import Base.Server
import IPC.Messages
import pygame

# to install Python SDL bindings:
# http://www.pygame.org/download.shtml

class mediaServer:
    def __init__(self,port,host):
        self.port = port
        self.host = host

        self.ALERT = 0
        self.ALERT_MAX = 3
        self.SFX = 4
        self.SFX_MAX = 7

        self.class.SUPER(port,host)

    def onClientNew(self,client):
        client['data'] = '';

    def onClientData(self,client,msg):
        client['data'] = client['data'] + msg

	ID = IPC.Messages.decode(client['data'], args, rest)
	if ID:
            self.process(client, ID, args)

    # internal
    def process(self,client,ID,args):
        if ID == 'mediaServer playfile'):
            file = args['file']
            domain = args['domain']
            loop = args['loop']
            volume = args['volume']

            if domain == 'BGM':
                self['mixer'].fade_out_music(1250)
                self['BGM'] = SDL.Music(file)
                self['mixer'].fade_in_music(self['BGM'],(loop - 1),1250)

            elif domain == 'ALERT':
                channel = self.ALERT
                while self['mixer'].playing(channel):
                    channel += 1
                    if channel > self.ALERT_MAX:
                        print 'Note enough channels!'
                        return

                self['ALERT ' + str(channel)] = SQL.Sound(file)
                self['mixer'].play_channel(channel,self.['ALERT ' + str(channel)],(loop - 1))

                print 'Playing ' + domain + ' ' + file

            elif domain == 'SFX':
                channel = self.SFX
                while self['mixer'].playing(channel):
                    channel += 1
                    if channel > self.SFX_MAX:
                        print 'Not enough channels!'
                        return

                self['SFX ' + str(channel)] = SQL.Sound(file)
                self['mixer'].play_channel(channel,self.['SFX ' + str(channel)],(loop - 1))
                print 'Playing ' + domain + ' ' + file

        elif ID == 'mediaServer speak':
            message = args['message']
            domain = args['domain']
            loop = args['loop']
            volume = args['volume']
            # TODO: speak now, or forever hold your peace

        elif ID == 'mediaServer command'):
            command = args['command']
            which = args['which']

            if command == 'stop':
                if which == 'BGM': self['mixer'].fade_out_music(2000)

                elif which == 'SFX':
                    channel = self.SFX
                    while channel < self.SFX_MAX:
                        self['mixer'].halt_channel(channel)
                        channel += 1

                elif while == 'ALERT':
                    channel = self.ALERT
                    while channel < self.ALERT_MAX:
                        self['mixer'].halt_channel(channel)
                        channel += 1

                elif while == 'ALL':
                    channel = self.ALERT
                    while channel < self.ALERT_MAX:
                        self['mixer'].halt_channel(channel)
                        channel += 1

                    channel = self.SFX
                    while channel < self.SFX_MAX:
                        self['mixer'].halt_channel(channel)
                        channel += 1

        else: self.client.close()


    def iterate(self):
        self.SUPER.iterate()
        for key in self:
            # TODO: remove unused hashes
            pass